<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Document [
  <!ENTITY ppl  "http://example.com/people#">
  <!ENTITY cpt  "http://example.com/concepts#">
  <!ENTITY dc   "http://purl.org/dc/terms/">
  <!ENTITY rif  "http://www.w3.org/2007/rif#">
  <!ENTITY func "http://www.w3.org/2007/rif-builtin-function#">
  <!ENTITY pred "http://www.w3.org/2007/rif-builtin-predicate#">
  <!ENTITY xs   "http://www.w3.org/2001/XMLSchema#">
  <!ENTITY act "http://www.w3.org/2007/rif-builtin-action#">
]>
<Document
    xmlns="http://www.w3.org/2007/rif#"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <payload>
    <Group>
      <sentence>
        <Group>
          <id>
            <Const type="xsd:string">startSort</Const>
          </id>
          <sentence>
            <Forall>
              <declare>
                <Var>c</Var>
              </declare>
              <formula>
                <Implies>
                  <if>
                    <Exists>
                      <declare>
                        <Var>Id</Var>
                      </declare>
                      <formula>
                        <And>
                          <formula>
                            <Member>
                              <instance>
                                <Var>c</Var>
                              </instance>
                              <class>
                                <Const type="rif:iri">http://example.com/2009/prd#sort.Control</Const>
                              </class>
                            </Member>
                          </formula>
                          <formula>
                            <Frame>
                              <object>
                                <Var>c</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">id</Const>
                                <Var>Id</Var>
                              </slot>
                            </Frame>
                          </formula>
                          <formula>
                            <External>
                              <content>
                                <Atom>
                                  <op>
                                    <Const type="rif:iri">&pred;numeric-equal</Const>
                                  </op>
                                  <args>
                                    <Var>Id</Var>
                                    <Const type="integer">InitTask</Const>
                                  </args>
                                </Atom>
                              </content>
                            </External>
                          </formula>
                        </And>
                      </formula>
                    </Exists>
                  </if>
                  <then>
                    <Do>
                      <actions ordered="yes">
                        <Modify>
                          <target>
                            <Frame>
                              <object>
                                <Var>c</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">id</Const>
                                <Const type="xsd:integer">SortTask</Const>
                              </slot>
                            </Frame>
                          </target>
                        </Modify>
                      </actions>
                    </Do>
                  </then>
                </Implies>
              </formula>
            </Forall>
          </sentence>
        </Group>
      </sentence>
      <sentence>
        <Group>
          <id>
            <Const type="xsd:string">startDisplay</Const>
          </id>
          <sentence>
            <Forall>
              <declare>
                <Var>c</Var>
              </declare>
              <formula>
                <Implies>
                  <if>
                    <Exists>
                      <declare>
                        <Var>Id</Var>
                      </declare>
                      <formula>
                        <And>
                          <formula>
                            <Member>
                              <instance>
                                <Var>c</Var>
                              </instance>
                              <class>
                                <Const type="rif:iri">http://example.com/2009/prd#sort.Control</Const>
                              </class>
                            </Member>
                          </formula>
                          <formula>
                            <Frame>
                              <object>
                                <Var>c</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">id</Const>
                                <Var>Id</Var>
                              </slot>
                            </Frame>
                          </formula>
                          <formula>
                            <External>
                              <content>
                                <Atom>
                                  <op>
                                    <Const type="rif:iri">&pred;numeric-equal</Const>
                                  </op>
                                  <args>
                                    <Var>Id</Var>
                                    <Const type="integer">SortTask</Const>
                                  </args>
                                </Atom>
                              </content>
                            </External>
                          </formula>
                        </And>
                      </formula>
                    </Exists>
                  </if>
                  <then>
                    <Do>
                      <actions ordered="yes">
                        <Modify>
                          <target>
                            <Frame>
                              <object>
                                <Var>c</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">id</Const>
                                <Const type="xsd:integer">DisplayTask</Const>
                              </slot>
                            </Frame>
                          </target>
                        </Modify>
                      </actions>
                    </Do>
                  </then>
                </Implies>
              </formula>
            </Forall>
          </sentence>
        </Group>
      </sentence>
      <sentence>
        <Group>
          <id>
            <Const type="xsd:string">switchPosition</Const>
          </id>
          <sentence>
            <Forall>
              <declare>
                <Var>first</Var>
              </declare>
              <declare>
                <Var>second</Var>
              </declare>
              <declare>
                <Var>c</Var>
              </declare>
              <formula>
                <Implies>
                  <if>
                    <And>
                      <formula>
                        <Exists>
                          <declare>
                            <Var>Id</Var>
                          </declare>
                          <declare>
                            <Var>p1</Var>
                          </declare>
                          <declare>
                            <Var>p2</Var>
                          </declare>
                          <declare>
                            <Var>v1</Var>
                          </declare>
                          <declare>
                            <Var>v2</Var>
                          </declare>
                          <formula>
                            <And>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>c</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Control</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>c</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">id</Const>
                                    <Var>Id</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-equal</Const>
                                      </op>
                                      <args>
                                        <Var>Id</Var>
                                        <Const type="integer"> SortTask </Const>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>first</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Element</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>first</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">position</Const>
                                    <Var>p1</Var>
                                  </slot>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">value</Const>
                                    <Var>v1</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>second</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Element</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>second</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">value</Const>
                                    <Var>v2</Var>
                                  </slot>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">position</Const>
                                    <Var>p2</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-greater-than</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Var>v2</Var>
                                        <Var>first.value</Var>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-less-than</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Var>p2</Var>
                                        <Var>first.position</Var>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                            </And>
                          </formula>
                        </Exists>
                      </formula>
                    </And>
                  </if>
                  <then>
                    <Do>
                      <actions ordered="yes">
                        <Modify>
                          <target>
                            <Frame>
                              <object>
                                <Var>first</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">position</Const>
                                <Var>p2</Var>
                              </slot>
                            </Frame>
                          </target>
                        </Modify>
                        <Modify>
                          <target>
                            <Frame>
                              <object>
                                <Var>second</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">position</Const>
                                <Var>p1</Var>
                              </slot>
                            </Frame>
                          </target>
                        </Modify>
                      </actions>
                    </Do>
                  </then>
                </Implies>
              </formula>
            </Forall>
          </sentence>
        </Group>
      </sentence>
      <sentence>
        <Group>
          <id>
            <Const type="xsd:string">incrementPosition</Const>
          </id>
          <sentence>
            <Forall>
              <declare>
                <Var>e1</Var>
              </declare>
              <declare>
                <Var>e2</Var>
              </declare>
              <declare>
                <Var>c</Var>
              </declare>
              <formula>
                <Implies>
                  <if>
                    <And>
                      <formula>
                        <Exists>
                          <declare>
                            <Var>Id</Var>
                          </declare>
                          <declare>
                            <Var>p1</Var>
                          </declare>
                          <declare>
                            <Var> p2 </Var>
                          </declare>
                          <declare>
                            <Var>v1</Var>
                          </declare>
                          <declare>
                            <Var>v2</Var>
                          </declare>
                          <formula>
                            <And>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>c</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Control</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>c</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">id</Const>
                                    <Var>Id</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-equal</Const>
                                      </op>
                                      <args>
                                        <Var>Id</Var>
                                        <Const type="integer">SortTask</Const>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>e1</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Element</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>e2</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Element</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>e2</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">value</Const>
                                    <Var>v2</Var>
                                  </slot>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">position</Const>
                                    <Var>p2</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-greater-than</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Var>v2</Var>
                                        <Var>e1.value</Var>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-equal</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Var>p2</Var>
                                        <Var>e1.position</Var>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                            </And>
                          </formula>
                        </Exists>
                      </formula>
                    </And>
                  </if>
                  <then>
                    <Do>
                      <actions ordered="yes">
                        <Modify>
                          <target>
                            <Frame>
                              <object>
                                <Var>e2</Var>
                              </object>
                              <slot ordered="yes">
                                <Const type="rif:iri">position</Const>
                                <External>
                                  <content>
                                    <Expr>
                                      <op>
                                        <Const type="rif:iri">&func;numeric-add</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Var>e1.position</Var>
                                        <Const type="xsd:integer">1</Const>
                                      </args>
                                    </Expr>
                                  </content>
                                </External>
                              </slot>
                            </Frame>
                          </target>
                        </Modify>
                      </actions>
                    </Do>
                  </then>
                </Implies>
              </formula>
            </Forall>
          </sentence>
        </Group>
      </sentence>
      <sentence>
        <Group>
          <id>
            <Const type="xsd:string">displayElement</Const>
          </id>
          <sentence>
            <Forall>
              <declare>
                <Var>c</Var>
              </declare>
              <declare>
                <Var>e</Var>
              </declare>
              <formula>
                <Implies>
                  <if>
                    <And>
                      <formula>
                        <Exists>
                          <declare>
                            <Var>Id</Var>
                          </declare>
                          <formula>
                            <And>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>c</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Control</Const>
                                  </class>
                                </Member>
                              </formula>
                              <formula>
                                <Frame>
                                  <object>
                                    <Var>c</Var>
                                  </object>
                                  <slot ordered="yes">
                                    <Const type="rif:iri">id</Const>
                                    <Var>Id</Var>
                                  </slot>
                                </Frame>
                              </formula>
                              <formula>
                                <External>
                                  <content>
                                    <Atom>
                                      <op>
                                        <Const type="rif:iri">&pred;numeric-equal</Const>
                                      </op>
                                      <args>
                                        <Var>Id</Var>
                                        <Const type="xsd:integer">DisplayTask</Const>
                                      </args>
                                    </Atom>
                                  </content>
                                </External>
                              </formula>
                              <formula>
                                <Member>
                                  <instance>
                                    <Var>e</Var>
                                  </instance>
                                  <class>
                                    <Const type="rif:iri">http://example.com/2009/prd#sort.Element</Const>
                                  </class>
                                </Member>
                              </formula>
                            </And>
                          </formula>
                        </Exists>
                      </formula>
                    </And>
                  </if>
                  <then>
                    <Do>
                      <actionVar>
                        <Var>v</Var>
                        <Frame>
                          <object>
                            <Var>e</Var>
                          </object>
                          <slot>
                            <Const type="rif:iri">value</Const>
                            <Var>v</Var>
                          </slot>
                        </Frame>
                      </actionVar>
                      <actionVar>
                        <Var>p</Var>
                        <Frame>
                          <object>
                            <Var>e</Var>
                          </object>
                          <slot>
                            <Const type="rif:iri">position</Const>
                            <Var>p</Var>
                          </slot>
                        </Frame>
                      </actionVar>
                      <actions>
                        <Execute>
                          <target>
                            <Atom>
                              <op>
                                <Const type="rif:iri">&act;print</Const>
                              </op>
                              <args ordered="yes">
                                <External>
                                  <content>
                                    <Expr>
                                      <op>
                                        <Const type="rif:iri">&func;concat</Const>
                                      </op>
                                      <args ordered="yes">
                                        <Const type="xsd:string">value</Const>
                                        <Var>v</Var>
                                        <Const type="xsd:string">is at position</Const>
                                        <Var>p</Var>
                                      </args>
                                    </Expr>
                                  </content>
                                </External>
                              </args>
                            </Atom>
                          </target>
                        </Execute>
                      </actions>
                    </Do>
                  </then>
                </Implies>
              </formula>
            </Forall>
          </sentence>
        </Group>
      </sentence>
    </Group>
  </payload>
</Document>
